<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Cause;
use App\Admin\Repositories\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Controllers\AdminController;

class CauseController extends AdminController
{

    public function index(Content $content)
    {
        return $content->title('请假')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Cause(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user_id','请假姓名')->display(function ($value){
               $res = \App\Models\User::where('id',$value)->value('user_name');
              return $res ?? '';
            });
            $grid->column('causation');

            $grid->column('start_date','开始日期')->display(function ($value){
                return date('Y-m-d H:i:s',$value);
            });
            $grid->column('end_date','结束日期')->display(function ($value){
                return date('Y-m-d H:i:s',$value);
            });
//            $grid->img('请假证明')->display(function ($pictures) {
//                return json_decode($pictures, true);
//            })->image($pre_img,80,80);
//            $grid->column('type','请假类型')->display(function ($value){
//                if ($value == 1){
//                    return '个人请假';
//                }else{
//                    return '重大事假';
//                }
//            });
//            $grid->column('created_at')->display(function ($value){
//                 return date('Y-m-d H:i:s',$value);
//            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
//                $actions->disableDelete();
                $actions->disableEdit();
//                $actions->disableQuickEidt();
                $actions->disableView();
            });
            $grid->disableCreateButton();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Cause(), function (Show $show) {
            $show->field('id');
            $show->field('user_name');
            $show->field('causation');
            $show->field('type');
            $show->field('img');
            $show->field('created_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Cause(), function (Form $form) {
            $form->display('id');
            $form->text('causation');
            $form->datetime('start_date','请假日期');
            $form->datetime('end_date','结束日期');
        });
    }
}
